
package com.dao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Service;

import com.model.Gz;
import com.model.Tz;
import com.model.User;


@Service("ComonDao")
public class ComonDao extends JdbcDaoSupport{
	@Resource
	public void setJb(JdbcTemplate jdbctemplate) {
		super.setJdbcTemplate(jdbctemplate);
	}
	public int getTotalUser() {
		String sql="select count(*) from user";
		return this.getJdbcTemplate().queryForInt(sql);
	}
	public List<User> findUserByPage(String page) {
		List<User> users=new ArrayList<User>();
		List<Map<String, Object>> list=this.getJdbcTemplate().queryForList("select * from user order by createTime desc limit "+(Integer.valueOf(page)-1)*10+",10");
		for(Map<String,Object> map:list){
			User u=new User();
			u.setId((String)map.get("id"));
			u.setUsername((String)map.get("username"));
			u.setSjhm((String)map.get("sjhm"));
			u.setBm((String)map.get("bm"));
			users.add(u);
		}
		return users;
	}
	public void saveUser(User user) {
		// TODO Auto-generated method stub
		String sql="insert into user(id,username,password,bm,sjhm,createTime) values('"+user.getId()+"','"+user.getUsername()+"','"+user.getPassword()+"','"+user.getBm()+"','"+user.getSjhm()+"','"+user.getCreateTime()+"')";
		this.getJdbcTemplate().execute(sql);
	}
	public User getUserById(String id) {
		List<Map<String, Object>> list=this.getJdbcTemplate().queryForList("select * from user where id = '"+id+"'");
		User u=new User();
		u.setId((String)list.get(0).get("id"));
		u.setUsername((String)list.get(0).get("username"));
		u.setPassword((String)list.get(0).get("password"));
		u.setSjhm((String)list.get(0).get("sjhm"));
		u.setBm((String)list.get(0).get("bm"));
		return u;
	}
	public void updateUser(User user) {
		String sql="update user set password= '"+user.getPassword()+"',bm = '"+user.getBm()+"',sjhm = '"+user.getSjhm()+"',createTime = '"+new SimpleDateFormat("yyyy-MM-dd").format(new Date())+"' where id = '"+user.getId()+"'";
		this.getJdbcTemplate().execute(sql);
		
	}
	public void deleteUser(String[] split) {
		String sql;
		for(int i=0;i<split.length;i++){
			sql="delete from user where id = '"+split[i]+"'";
			this.getJdbcTemplate().execute(sql);
		}
	}
	public void setJcData(String jdip, Map<String, List<String>> map) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<Map<String, Object>> list=this.getJdbcTemplate().queryForList("select * from admin_cpujc where ip = '"+jdip+"' order by time desc limit 0,100");
		List<Map<String, Object>> list1=this.getJdbcTemplate().queryForList("select * from admin_ncjc where ip = '"+jdip+"' order by time desc limit 0,100");
		List<Map<String, Object>> list2=this.getJdbcTemplate().queryForList("select * from admin_ypjc where ip = '"+jdip+"' order by time desc limit 0,100");
		List<String> data1=new ArrayList<String>();
		List<String> data2=new ArrayList<String>();
		List<String> data3=new ArrayList<String>();
		List<String> data4=new ArrayList<String>();
		List<String> data5=new ArrayList<String>();
		List<String> data6=new ArrayList<String>();
		for(int i=list.size()-1;i>=0;i--){
			data1.add(df.format(list.get(i).get("time")));
			data2.add((String)list.get(i).get("dqz"));
		}
		for(int i=list1.size()-1;i>=0;i--){
			data3.add(df.format(list1.get(i).get("time")));
			data4.add((String)list1.get(i).get("dqz"));
		}
		for(int i=list2.size()-1;i>=0;i--){
			data5.add(df.format(list2.get(i).get("time")));
			data6.add((String)list2.get(i).get("dqz"));
		}
		map.put("data1", data1);
		map.put("data2", data2);
		map.put("data3", data3);
		map.put("data4", data4);
		map.put("data5", data5);
		map.put("data6", data6);
	}
	public void setJcData(String jdip, String startTime, String endTime, Map<String, List<String>> map) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<Map<String, Object>> list=this.getJdbcTemplate().queryForList("select * from admin_cpujc where ip = '"+jdip+"' and time > '"+startTime+"' and time < '"+endTime+"' order by time desc");
		List<Map<String, Object>> list1=this.getJdbcTemplate().queryForList("select * from admin_ncjc where ip = '"+jdip+"' and time > '"+startTime+"' and time < '"+endTime+"' order by time desc");
		List<Map<String, Object>> list2=this.getJdbcTemplate().queryForList("select * from admin_ypjc where ip = '"+jdip+"' and time > '"+startTime+"' and time < '"+endTime+"' order by time desc");
		List<String> data1=new ArrayList<String>();
		List<String> data2=new ArrayList<String>();
		List<String> data3=new ArrayList<String>();
		List<String> data4=new ArrayList<String>();
		List<String> data5=new ArrayList<String>();
		List<String> data6=new ArrayList<String>();
		for(int i=list.size()-1;i>=0;i--){
			data1.add(df.format(list.get(i).get("time")));
			data2.add((String)list.get(i).get("dqz"));
		}
		for(int i=list1.size()-1;i>=0;i--){
			data3.add(df.format(list1.get(i).get("time")));
			data4.add((String)list1.get(i).get("dqz"));
		}
		for(int i=list2.size()-1;i>=0;i--){
			data5.add(df.format(list2.get(i).get("time")));
			data6.add((String)list2.get(i).get("dqz"));
		}
		map.put("data1", data1);
		map.put("data2", data2);
		map.put("data3", data3);
		map.put("data4", data4);
		map.put("data5", data5);
		map.put("data6", data6);
	}
	public void saveGz(Gz gz) {
		String sql="insert into gzb(id,gzlb,sl,sj,bm,createTime) values('"+gz.getId()+"',"+gz.getGzlb()+","+gz.getSl()+",'"+gz.getSj()+"','"+gz.getBm()+"','"+gz.getCreateTime()+"')";
		this.getJdbcTemplate().execute(sql);
	}
	public int getTotalGz(String gzlb) {
		String sql="select count(*) from gzb where gzlb = '"+gzlb+"'";
		return this.getJdbcTemplate().queryForInt(sql);
	}
	public List<Gz> findGzByPage(String page, String gzlb) {
		List<Gz> users=new ArrayList<Gz>();
		List<Map<String, Object>> list=this.getJdbcTemplate().queryForList("select * from gzb where gzlb = '"+gzlb+"' order by createTime desc limit "+(Integer.valueOf(page)-1)*10+",10");
		for(Map<String,Object> map:list){
			Gz u=new Gz();
			u.setId((String)map.get("id"));
			u.setBm((String)map.get("bm"));
			u.setSj((String)map.get("sj"));
			u.setSl(map.get("sl").toString());
			users.add(u);
		}
		return users;
	}
	public Gz getGzById(String id) {
		Gz gz=new Gz();
		List<Map<String, Object>> list=this.getJdbcTemplate().queryForList("select * from gzb where id = '"+id+"'");
		gz.setId((String)list.get(0).get("id"));
		gz.setBm((String)list.get(0).get("bm"));
		gz.setSj((String)list.get(0).get("sj"));
		gz.setSl(list.get(0).get("sl").toString());
		return gz;
	}
	public void updateGz(Gz gz) {
   String sql="update gzb set bm='"+gz.getBm()+"',sj='"+gz.getSj()+"',sl="+gz.getSl()+",createTime='"+gz.getCreateTime()+"' where id='"+gz.getId()+"'";		
   this.getJdbcTemplate().execute(sql);
	}
	
	public void deleteGz(String[] split) {
		String sql;
		for(int i=0;i<split.length;i++){
			sql="delete from gzb where id = '"+split[i]+"'";
			this.getJdbcTemplate().execute(sql);
		}		
	}
	public int getTotalTz() {
		String sql="select count(*) from txinfo";
		return this.getJdbcTemplate().queryForInt(sql);
	}
	public List<Tz> findTzByPage(String page) {
		List<Tz> users=new ArrayList<Tz>();
		List<Map<String, Object>> list=this.getJdbcTemplate().queryForList("select * from txinfo  order by sj desc limit "+(Integer.valueOf(page)-1)*10+",10");
		SimpleDateFormat sd=new  SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for(Map<String,Object> map:list){
			Tz u=new Tz();
			u.setId(map.get("id").toString());
			u.setBt((String)map.get("bt"));
			u.setNr((String)map.get("nr"));
			u.setZt(map.get("zt").toString());
			u.setSj(sd.format(map.get("sj")));
			users.add(u);
		}
		return users;
	}
	public boolean login(String username, String password) {
		List<Map<String, Object>> list=this.getJdbcTemplate().queryForList("select * from user where username = '"+username+"' and password = '"+password+"'");
		if(list.size()>0){
			return true;
		}
		return false;
	}

}
